// Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.

// 准备资源账号
package resdomain

import (
	"fleetmanager/db/dao"
	"fleetmanager/workflow/components"
	"fleetmanager/workflow/directer"
	"fleetmanager/workflow/meta"
)

type PrepareResDomainTask struct {
	components.BaseTask
}

// Execute 执行资源账号同步任务
func (t *PrepareResDomainTask) Execute(ctx *directer.ExecuteContext) (output interface{}, err error) {
	defer func() { t.ExecNext(output, err) }()
	ori := t.Directer.GetContext().Get(directer.WfKeyOriProjectId).ToString("")
	u, err := dao.GetUserResConfByOriProjectId(ori)
	if err != nil {
		return nil, err
	}

	// 获取资源租户Keypair名称
	keypair := u.KeyPairName
	if err = t.Directer.GetContext().Set(directer.WfKeyResourceKeypairName, keypair); err != nil {
		return nil, err
	}
	if err = t.Directer.GetContext().Set(directer.WfKeyProjectId, u.ProjectId); err != nil {
		return nil, err
	}
	if err = t.Directer.GetContext().Set(directer.WfKeyAccessKey, u.AccessKey); err != nil {
		return nil, err
	}
	if err = t.Directer.GetContext().Set(directer.WfKeySecurityAccessKey, u.SecretAccessKey); err != nil {
		return nil, err
	}
	if err = t.Directer.GetContext().Set(directer.WfKeyIamAgencyName, u.AgencyName); err != nil {
		return nil, err
	}
	if err = t.Directer.GetContext().Set(directer.WfKeyAuxProxyPath, u.AuxProxyPath); err != nil {
		return nil, err
	}
	if err = t.Directer.GetContext().Set(directer.WfKeyBuildScriptPath, u.BuildScriptPath); err != nil {
		return nil, err
	}
	if err = t.Directer.GetContext().Set(directer.WfKeyDockerBuildScriptPath, u.DockerBuildScriptPath); err != nil {
		return nil, err
	}
	if err = t.Directer.GetContext().Set(directer.WfKeyBuildFileStorageRegion, u.FileStorageRegion); err != nil {
		return nil, err
	}
	return nil, nil
}

// NewPrepareResDomainTask 新建准备资源租户任务
func NewPrepareResDomainTask(meta meta.TaskMeta, directer directer.Directer, step int) components.Task {
	t := &PrepareResDomainTask{
		components.NewBaseTask(meta, directer, step),
	}

	return t
}
